package com.hopedove.productionworkshop

import androidx.databinding.ObservableField
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import com.hadilq.liveevent.LiveEvent
import com.hopedove.callsystem.activity.call.CallMainActivity
import com.hopedove.data.AppUpdateInfo
import com.hopedove.net.OkResponse
import com.hopedove.net.domain.Communication
import com.hopedove.net.domain.GetNewVersion
import com.hopedove.quanlitycheck.activity.LoginActivity
import com.hopedove.repository.CommonRepository
import com.hopedove.returnyarnsystem.activity.ReturnYarnMainActivity
import com.hopedove.tempdemo.QueryNoticeList
import com.hopedove.utils.ParseUtils
import com.hopedove.viewmodel.BaseViewModel
import com.hopedove.workshop.ui.WorkshopLoginActivity
import com.hopedove.yieldmanage.activity.ChangziQueryActivity
import com.hopedove.yieldmanage.activity.YieldLoginActivity
import com.hopedove.yieldmanage.entity.UpdateInfoBizData
import kotlinx.coroutines.launch
import org.json.JSONObject

class AppHomePageViewModel(private val commonRepo: CommonRepository) :
    BaseViewModel() {


    private val _updateInfo = LiveEvent<AppUpdateInfo>()
    val updateInfo: LiveData<AppUpdateInfo> = _updateInfo

    val appVersion = ObservableField("当前版本：${commonRepo.getAppVersionName()}")


    override fun load() = viewModelScope.launch {
        checkAppUpdate()
        commonRepo.getServerDateTime(true)
    }

    // 点击 生产车间
    fun onWorkShopClick() = navigateTo(WorkshopLoginActivity::class.java)

    // 点击 工艺
    fun craftOnClick() = navigateTo(QueryNoticeList::class.java)

    // 点击 织造查询
    fun changziOnClick() = navigateTo(ChangziQueryActivity::class.java)

    // 点击 呼叫保全
    fun callOnClick() = navigateTo(CallMainActivity::class.java)

    // 点击 退沙
    fun returnYarnOnClick() = navigateTo(ReturnYarnMainActivity::class.java)

    // 点击 翻改首匹
    fun qualityCheckClick() = navigateTo(LoginActivity::class.java)

    // 点击 织造产量
    fun yieldOnClick() = navigateTo(YieldLoginActivity::class.java)

    // 检查 App 更新
    fun checkAppUpdate() = viewModelScope.launch {
        _loading.value = true
        Communication.asyncRequest(GetNewVersion("生产车间app")) { r ->
            _loading.value = false
            if (r is OkResponse && !BuildConfig.DEBUG) {
                val jsonObj = JSONObject(r.data)
                val data = jsonObj.optJSONObject("DATA")
                data?.let {
                    val updateInfo = ParseUtils.parse(it.toString(), UpdateInfoBizData::class.java)
                    if (commonRepo.getAppVersionName() != updateInfo.androidversionCode) {
                        val apk = commonRepo.newDownloadApkPathForUpdate()
                        _updateInfo.value = AppUpdateInfo(
                            updateInfo.androidForced == "1",
                            updateInfo.androidUrl,
                            apk.absolutePath
                        )
                    }
                }
            }
        }
    }

}